<%@ page import="vy.org.app.common.*" %>
<%@ page import="org.vy.dal.dao.*" %>
<%@ page import="java.util.*" %>
<%@ page import="org.vy.dal.dao.lookup.ViewVyLookupCitiesDO" %>
<%@ page import="vy.org.app.page.printlabel.group.GroupPrintLabelForm" %>
<%@ taglib uri="/WEB-INF/lib/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/lib/struts-bean.tld" prefix="bean"%>

<%!
	private static final String EMPTY = "&nbsp;";
	private UserCtx m_user;
%>

<%
	ResourceBundle RESOURCE = ResourceBundle.getBundle("LabelResource",ActionCtx.ctx().getLocale());
	m_user = ActionCtx.ctx(request).getUser();
  	boolean editSubscription = m_user.hasAccessToModule(ModuleEnum.EDIT_SUBSCRIPTION.getId().toString());
%>
<bean:define id="groupPrintLabelForm" name="groupPrintLabelForm" scope="request" type="vy.org.app.page.printlabel.group.GroupPrintLabelForm"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"/> 
	<link rel="stylesheet" type="text/css" href="./css/Common.css"/>
	<script type="text/javascript" src="./scripts/Common.js"></script>
	<script type="text/javascript" src="./scripts/KrutidevUnicode.js"></script>
</head> 
<body>
<form name="groupPrintLabelForm" method=post action="./saveGroupPrintLabel.do">
  <input type="hidden" name="ops" id="ops" value="<bean:write name='groupPrintLabelForm' property='ops' />">
  <input type="hidden" name="lang" id="lang" value="<%=ActionCtx.ctx().getLang() %>">
  <table class="qry" width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
      <td></td>
      <td><img src="./pages/image_gif/mf1.gif" width="861" height="35"></td>
      <td></td>
    </tr>
    <tr>
      <td align="right" valign="middle" width="3" ></td>
      <td width="850px"  align="left" valign="top">
	      <noscript><div class="err">Kindly enable javascript in your browser</div></noscript> 
      </td>
      <td></td>
    </tr>
    <tr>
      <td colspan="3"></td>
    </tr>
  </table>
  
  <div class="abtn-cnt">
  <%
	if(editSubscription){
		if("add".equals(groupPrintLabelForm.getOps())){
	  		%><a href="javascript:;" id="aGroupPrintLabel" name="aGroupPrintLabel" class="abtn" title="Add To" onclick="addToGroup();"><%=RESOURCE.getString("ADD_TO") %></a><%
	  		%><a href="javascript:;" id="cnGroupPrintLabel" name="cnGroupPrintLabel" class="abtn" title="Create New" onclick="createNewGroup();"><%=RESOURCE.getString("CREATE_NEW") %></a><%
		}else{
	  		%><a href="javascript:;" id="rGroupPrintLabel" name="rGroupPrintLabel" class="abtn" title="Remove" onclick="removeFromGroup();"><%=RESOURCE.getString("DELETE") %></a><%
		}
  	}
  %>
  </div>
  <%
  	if(request.getAttribute(AppConstants.RESULT) != null){
	%>  
  <div class="rst-panel" style="width:auto;">  
  <table class="rst" cellpadding="0" cellspacing="0" width="100%" border="0">
  	<thead>
  		<tr>
  			<th><div class="hd"><%=RESOURCE.getString("SELECT") %></div></th>
  			<th><div class="hd">Label Address</div></th>
  			<th><div class="hd"><%=RESOURCE.getString("GROUP_ID") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("VY_ID") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("FIRST_NAME") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("MIDDLE_NAME") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("LAST_NAME") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("REGIONAL_LANG_NAME") %></div></th>
  			<th><div class="hd"><%=RESOURCE.getString("REF_VY_ID") %></div></th>
  		</tr>
  	</thead>
  	<tbody>
<%
		List<GroupPrintLabelForm> results = (List<GroupPrintLabelForm>)request.getAttribute(AppConstants.RESULT);
  		int idx=0;
  		for(GroupPrintLabelForm rec : results){
  			String addr = rec.getLabelAddress();
  			idx++;
  			%><tr id="<%="rec_"+idx%>">
  				<% if("add".equals(groupPrintLabelForm.getOps())) { %>
				<td style="text-align:center;"><a href="javascript:;" onclick="remove('<%="rec_"+idx%>');" ><img src="./pages/images/trash_1.png" border="0" height="17"></a><input type="hidden" name="ID" value="<%=rec.getVyId() %>" /></td>
				<%} else{ %>
  				<td><input type="checkbox" name="ID" value="<%=rec.getVyId() %>" /></td>
  				<% } %>
				<td>
				<% if("add".equals(groupPrintLabelForm.getOps())){ %>
					<input type="radio" <%=(rec.getSubscriptionId().equals(rec.getLabelAddress())? "checked=\"checked\"":"") %> name="labelAddress" id="<%="labelAddress_"+idx%>" value="<%=rec.getSubscriptionId() %>" />
				<% } else {%>
					<%=(rec.getSubscriptionId().equals(rec.getLabelAddress())? "Yes":"") %>
				<% } %>
					<input type="hidden" name="subId" id="subId" value="<%=rec.getSubscriptionId() %>" />
				</td>
				<td><%=rec.getGroupId()!=null? rec.getGroupId() : EMPTY %></td>
				<td><%=rec.getVyId()!=null? rec.getVyId() : EMPTY %></td>
				<td><%=rec.getFirstName()!=null? rec.getFirstName() : EMPTY %></td>
				<td><%=rec.getMiddleName()!=null?rec.getMiddleName() : EMPTY %></td>
				<td><%=rec.getLastName()!=null? rec.getLastName() : EMPTY %></td>
				<td class="kd010"><%=rec.getRegionalLangName()!=null? rec.getRegionalLangName() : EMPTY %></td>
				<td><%=rec.getRefVyId()!=null? rec.getRefVyId() : EMPTY %></td>
			</tr>
			<%
  		}
  		%>
  </tbody>
  </table>
 </div>
   <%
  	}else{
  		%><div><%=RESOURCE.getString("NO_RECORDS") %></div><%
  	}
  %>

</form>
<script>

	function addToGroup(){
		var cnt = document.getElementsByName("ID").length;
		if(cnt < 2){
			alert("For adding to existing group at least 2 members should be there");
			return;
		}
		if(confirm("This will add the members to the print group of selected address member,\nif print group not present it will create new one. \nOther's print group will get overwritten. Are you sure you want to proceed?")){
			var labelAddress = getCheckedValues("labelAddress");
			if(labelAddress.length == 0){
				alert("Please select at lease one label address");
			}else if(labelAddress.length > 1){
				alert("Only 1 address can be selected");
			}else{
				document.groupPrintLabelForm.action = "./saveGroupPrintLabel.do?"; 
				document.getElementById("ops").value="addToGroup";
				document.groupPrintLabelForm.submit();
			}
		}
	}

	function createNewGroup(){
		if(confirm("This will create new print group id and assign all members to that, any existing print group id will get overwritten. Are you sure you want to proceed?")){
			var labelAddress = getCheckedValues("labelAddress");
			if(labelAddress.length == 0){
				alert("Please select at lease one label address");
			}else if(labelAddress.length > 1){
				alert("Only 1 address can be selected");
			}else{
				document.groupPrintLabelForm.action = "./saveGroupPrintLabel.do?"; 
				document.getElementById("ops").value="createNewGroup";
				document.groupPrintLabelForm.submit();
			}
		}
	}

	function removeFromGroup(){
		var vals = getCheckedValues("ID");
		if(vals.length == 0){
			alert("Please select at lease one member");
		}else{
			var labelAddress = document.getElementsByName("labelAddress");
			var checks = document.getElementsByName("members");
			for(var i=0; i < checks.length; i++){
				if(checks[i].checked && labelAddress[i].value != ""){
					alert("Address cannot be selected for deletion member");
					return;
				}
			}
			labelAddress = getCheckedValues("labelAddress");
			//if(labelAddress.length == 0){
			//	alert("Please select at lease one valid member address");
			//}else if(labelAddress.length > 1){
			//	alert("Only 1 address can be selected");
			//}else{
				if(confirm("This will remove members from the print label group. Are you sure you want to proceed?")){
					document.groupPrintLabelForm.action = "./saveGroupPrintLabel.do?"; 
					document.getElementById("ops").value="removeFromGroup";
					document.groupPrintLabelForm.submit();
				}
			//}
		}
	}

	function remove(id) {
		var tr = document.getElementById(id);
		var tb = tr.parentNode;
		tb.removeChild(tr);
	}
</script>

</body> 
</html>
     
      
    
